*** Settings ***
Library           RequestsLibrary
Library           JSONLibrary
Resource          ../common/common.robot
Resource        ../goods/goods.robot
Resource        ../role/role.robot


*** Test Cases ***
Test Case: 测试商品详情有权限的可以查看批发价
    Login Admin Account
    # 准备数据
    ${data}    Load JSON from file    ./DEF-16742/data/save_goods_for_permission.json     UTF-8
    ${goodSn}  evaluate  random.randint(1000,9999)          #生成随机的商品sn
    ${data}    Update value to JSON     ${data}     $.goodsVO.goodsSn      ${goodSn}
    # 确保数据库至少有一个数据
    ${new_goods_id}     Save Goods    ${data}
    # 获得详情的批发价
    ${new_goods}    Get Goods    ${new_goods_id}
    # 取出批发价
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Compare Numbers Ignoring Format And Decimal Places   ${tradePrice[0]}    1
    # 删除商品
    Delete Goods   ${new_goods_id}

Test Case: 测试商品详情无权限的不可以查看批发价
    Login Admin Account
    # 准备数据
    ${data}    Load JSON from file    ./DEF-16742/data/save_goods_for_permission.json     UTF-8
    ${goodSn}  evaluate  random.randint(1000,9999)          #生成随机的商品sn
    ${data}    Update value to JSON     ${data}     $.goodsVO.goodsSn      ${goodSn}
    # 确保数据库至少有一个数据
    ${new_goods_id}     Save Goods    ${data}
    # 制作一个无查看批发价的账号
    ${role_data}    Load JSON from file    ./DEF-16742/data/save_role_for_no_permission.json     UTF-8
    # 新增一个无权限用户
    ${role_id}      Save Role       ${role_data}
    # 更新用户为无权限用户
    ${no_permission_account}    Load JSON from file    ./DEF-16742/data/update_user_role.json     UTF-8
    ${role_arr}     Create List         ${role_id}
    Log     ${role_arr}
    ${no_permission_account}    Update value to JSON     ${no_permission_account}     $.roleIds     ${role_arr}
    Update User Role     ${no_permission_account}
    # 登录这个无权限的用户
    Login Account    ${testUser}    ${testPassword}
    # 获得详情的批发价
    ${new_goods}    Get Goods    ${new_goods_id}
    # 取出批发价
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Should Be Empty    ${tradePrice}
    Login Admin Account
    # 删除商品
    Delete Goods   ${new_goods_id}
    ${role_arr}     Create List
    # 清空角色占用
    ${no_permission_account}    Update value to JSON     ${no_permission_account}     $.roleIds      ${role_arr}
    Update User Role    ${no_permission_account}
    
    # 删除这个测试角色
    Delete Role    ${role_id}

Test Case: 测试商品详情有权限的可以更新批发价
    Login Admin Account
    # 准备数据
    ${data}    Load JSON from file    ./DEF-16742/data/save_goods_for_permission.json     UTF-8
    ${goodSn}  evaluate  random.randint(1000,9999)          #生成随机的商品sn
    ${data}    Update value to JSON     ${data}     $.goodsVO.goodsSn      ${goodSn}
    # 确保数据库至少有一个数据
    ${new_goods_id}     Save Goods    ${data}
    # 获得详情的批发价
    ${new_goods}    Get Goods    ${new_goods_id}
    # 取出批发价
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Compare Numbers Ignoring Format And Decimal Places   ${tradePrice[0]}    1

    #    更新批发价为 2
    ${no_permission_account}    Update value to JSON     ${new_goods}     $.goodsDTO.tradePrice      2
    ${new_goods}    Get Goods    ${new_goods_id}
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Compare Numbers Ignoring Format And Decimal Places   ${tradePrice[0]}    2
    # 删除商品
    Delete Goods   ${new_goods_id}

Test Case: 测试商品详情无权限的不可以更新批发价
    Login Admin Account
    # 准备数据
    ${data}    Load JSON from file    ./DEF-16742/data/save_goods_for_permission.json     UTF-8
    ${goodSn}  evaluate  random.randint(1000,9999)          #生成随机的商品sn
    ${data}    Update value to JSON     ${data}     $.goodsVO.goodsSn      ${goodSn}
    # 确保数据库至少有一个数据
    ${new_goods_id}     Save Goods    ${data}
    ####   制作一个无权限的账号
    # 新增一个无权限角色
    ${role_data}    Load JSON from file    ./DEF-16742/data/role_only_get_goods_info.json     UTF-8
    ${role_data}    Update value to JSON     ${role_data}     $.roleInfo.name     ${goodSn}
    ${role_id}      Save Role       ${role_data}
    # 更新用户角色为无权限角色
    ${no_permission_account}    Load JSON from file    ./DEF-16742/data/update_user_role.json     UTF-8
    ${role_arr}     Create List         ${role_id}
    ${no_permission_account}    Update value to JSON     ${no_permission_account}     $.roleIds     ${role_arr}
    Update User Role     ${no_permission_account}

    # 获得详情的批发价
    ${new_goods}    Get Goods    ${new_goods_id}
    # 取出批发价
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Compare Numbers Ignoring Format And Decimal Places   ${tradePrice[0]}    1
    ${good_id}       Get Value From Json    ${new_goods}    $.goodsDTO.id
    # 登录这个无权限的用户
    Login Account    ${testUser}    ${testPassword}

    #    更新批发价为 2
    ${new_goods}    Load JSON from file    ./DEF-16742/data/update_goods_for_no_trade_price_permission.json     UTF-8
    ${new_goods}    Update value to JSON     ${new_goods}     $.goodsVO.tradePrice      2
    ${new_goods}    Update value to JSON     ${new_goods}     $.goodsVO.id      ${good_id[0]}
    ${new_goods}    Update value to JSON     ${new_goods}     $.goodsVO.goodsSn      ${goodSn}
    Update Goods    ${new_goods}

    Login Admin Account
    # 获得详情的批发价
    ${new_goods}    Get Goods    ${new_goods_id}
    # 取出批发价
    ${tradePrice}    Get Value From Json    ${new_goods}    $.goodsDTO.tradePrice
    Compare Numbers Ignoring Format And Decimal Places   ${tradePrice[0]}    1

    # 删除商品
    Delete Goods   ${new_goods_id}

    # 清空角色占用
    Clear User Role

    # 删除这个测试角色
    Delete Role    ${role_id}

Test Case: 清空脏数据
     Login Admin Account
     ${search_goods_condition}    Load JSON from file    ./DEF-16742/data/search_goods_condition.json     UTF-8
     ${list_goods_data}     List Goods    ${search_goods_condition}
     # 循环删除
     FOR    ${element}    IN    @{list_goods_data}
         Log    ${element['id']}
         Delete Goods   ${element['id']}
     END
